package com.my.content.near;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.my.content.bean.NearBean;
import com.my.content.liveproject.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 李雪斌 on 2016/12/19.
 **/
public class GridViewAdapter extends BaseAdapter {
    private Context context;
    private List<NearBean.FlowBean> data;
    private final LayoutInflater inflater;

    public GridViewAdapter(FragmentActivity activity) {
        this.context = activity;
        inflater = LayoutInflater.from(context);
    }

    //设置数据
    public void setData(List<NearBean.FlowBean> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return data != null ? data.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.near_grid_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //绑定数据
        NearBean.FlowBean flowBean = data.get(position);
        holder.gridItemKm.setText(flowBean.getInfo().getDistance());
        String img_url = flowBean.getInfo().getCreator().getPortrait();
        //根据获取的level值来设置主播的等级勋章
//        int level = flowBean.getInfo().getCreator().getLevel();
//        String ll = "rank_"+ level;
//        holder.gridItemLevel.setImageResource(R.mipmap.);
        //设置图片
//        Picasso.with(context).load(img_url).placeholder(R.mipmap.default_room).into(holder.gridItemIcon);
        Glide.with(context).load(img_url).placeholder(R.mipmap.default_room).into(holder.gridItemIcon);
        return convertView;
    }

//    @OnClick(R.id.gridItem_layout)
//    public void onClick(View view) {
//        switch (view.getId()) {
//            case R.id.gridItem_layout:
//                Toast.makeText(context, "暂无视频资源", Toast.LENGTH_SHORT).show();
//                break;
//        }
//    }


    static class ViewHolder {
        @BindView(R.id.gridItem_icon)
        ImageView gridItemIcon;
        @BindView(R.id.gridItem_level)
        ImageView gridItemLevel;
        @BindView(R.id.gridItem_km)
        TextView gridItemKm;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
